import {
	Body,
	Controller,
	DefaultValuePipe,
	Delete,
	Get,
	Param,
	ParseIntPipe,
	Patch,
	Post,
	Query,
	Request,
	UseGuards,
} from '@nestjs/common';
import { RoleService } from './role.service';
import { CreateRoleDto } from './dto/create-role.dto';
import { UpdateRoleDto } from './dto/update-role.dto';
import { ApiBearerAuth, ApiHeader, ApiOperation, ApiTags } from '@nestjs/swagger';
import { AuthGuard } from '@nestjs/passport';

@ApiBearerAuth() // 需要token
@UseGuards(AuthGuard('jwt'))
@ApiTags('角色管理')
@ApiHeader({
	name: 'authoriation',
	required: true,
	description: 'token',
})
@Controller({
	path: 'role',
	version: '1',
})
export class RoleController {
	constructor(private readonly roleService: RoleService) {}

	@ApiOperation({
		tags: ['创建角色'],
		description: '创建角色',
	})
	@Post()
	create(@Request() request, @Body() createRoleDto: CreateRoleDto) {
		const { userId } = request.user;
		return this.roleService.create(createRoleDto, userId);
	}

	@Get()
	findAll() {
		return this.roleService.findAll();
	}

	@ApiOperation({
		tags: ['查询角色列表'],
		description: '查询角色列表',
	})
	@Get('/getRoleList')
	findPermissionList(
		@Query('pageNum', new DefaultValuePipe(1), ParseIntPipe) pageNum: number,
		@Query('pageSize', new DefaultValuePipe(10), ParseIntPipe) pageSize: number,
		@Query('keyword', new DefaultValuePipe('')) keyword: string,
	) {
		return this.roleService.findListByPage({
			pageNum,
			pageSize,
			keyword,
		});
	}

	@Get(':id')
	findOne(@Param('id') id: string) {
		return this.roleService.findOne(+id);
	}

	@Patch(':id')
	update(@Param('id') id: string, @Body() updateRoleDto: UpdateRoleDto) {
		return this.roleService.update(+id, updateRoleDto);
	}

	@Delete(':id')
	remove(@Param('id') id: string) {
		return this.roleService.remove(+id);
	}
}
